$(document).ready(function(){  
	$("#functionNav").attr("href",navAddr);

	getUserRight();
	var roleid = userInfo.roleid;
	if(roleid.indexOf("zerenbaoguanyuan")>-1){
		$("#add_btn").show();
		$("#delete_btn").show();
	}else{
		$("#add_btn").hide();
		$("#delete_btn").hide();
	}
//	dxjbJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do", {"tableName":'jb_xfjb'});
//	dxjbJson = dxjbJson.rows;
//	loadSelect("#dxjb",dxjbJson,'id','name');
//	loadSelect("#dx_dxjb",dxjbJson,'id','name');
//	
//	dxyyJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do", {"tableName":'jb_xfyy'});
//	dxyyJson = dxyyJson.rows;
//	loadSelect("#dxyy",dxyyJson,'id','name');
//	loadSelect("#dx_dxyy",dxyyJson,'id','name');

	
	//获取并填充保管部门
	var bgbmJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do",{'tableName':'jb_bgbm'});
	loadSelect("#bgbm",bgbmJson.rows,'id','name');
	
	//根据保管部门id获取并填充库房
	$("#bgbm").change(function(){
		var bgbmid = $(this).val();
		if(bgbmid !=null && bgbmid !=''){
			var kfJson = ajaxResult("get",false,ProjectName+"/sysmanage.getkfIdNameBybgbmId.do",{'bgbmId':bgbmid});
			loadSelect("#szkf",kfJson.rows,'kfid','kfmc');
		}
	});
	  
	var params =$("#form1").serializeObject();
    //初始化出退库单据Table
	createTable('#cpxzTable',ProjectName+'/collimit.getColLimitList.do','id',dxcpTitle,params);	
	//点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#cpxzTable','#form1');
	 });
	 $("#add_btn").click(function(){
		 $('#model_zdjh').removeAttr("readonly");
		 $('#model_zljg').removeAttr("readonly");
		 $('#model_zltj').removeAttr("readonly");
		 $('#model_bctj').removeAttr("readonly");
		 $('#model_jzcg').removeAttr("disabled");
		 $('#model_bwcp').removeAttr("disabled");
		 $('#model_zdjh').focus();
		 $('#model_zdjh').val("");
		 $('#model_cpmc').val("");
		 $('#model_cpjb').val("");
		 $('#model_cpfl1').val("");
		 $('#model_zljg').val("0");
		 $('#model_zltj').val("");
		 $('#model_bctj').val("");
		 $('#model_jzcg').val("0");
		 $('#model_bwcp').val("0");
		 $('#model_id').val("");
		 $("#lrrid").val(userInfo.userid);
		 $("#lrrmc").val(userInfo.username);
		 $("#lrsj").val(nowDate());
		 $("#cpxzxxModal").modal('show');
	 });
	 $('#model_zdjh').blur(function(){
		 var zdjh =$('#model_zdjh').val();
		 if(zdjh==""){
			 alertFunc("请输入总登记号");
			 return;
		 }
		 var params = {"cols":"cpfl1,cpjb","zdjh":zdjh,"bgyid":userInfo.userid};  
		 var detail = ajaxResult("get",false,ProjectName+"/zzquery.getColListForZrbgy.do",params);
		 if(detail.status=="ok"){
			 var rows = detail.rows;
			 if(rows!=null&&rows.length>0){
				 var data = rows[0];
				 $('#model_cpmc').val(data.cpmc);
				 $('#model_cpjb').val(data.cpjb);
				 $('#model_cpfl1').val(data.cpfl1);
			 }else{
				 alertFunc("没有找到总登记号，或不归您管辖，请重新输入");
				 return;
			 }
		 }else{
			 alertFunc("没有找到总登记号，或不归您管辖，请重新输入");
			 return;
		 }
//		 if(detail.status=="ok"){
	 });
	 $("#delete_btn").click(function(){
		 var ids = selDataIds("#cpxzTable");
		 if(ids.length == 0){
			 alertFunc("请选择记录！");
			 return false;
		 }
		 bootbox.setLocale("zh_CN");
		 bootbox.confirm("您确定删除？",function(r){
			 if(r){
				 var detail = ajaxResult("get",false,ProjectName+"/collimit.delColLimit.do",{"id":ids+""});
				 if(detail.status=="ok"){
					 alertFunc("删除成功");
					 refreshTable('#cpxzTable','#form1');
				 }else{
					 alertFunc("删除失败");
				 }
			 }
		 });

	 });
	 $("#save_btn").click(function(){
		 var bwcp = $("#model_bwcp").val();
		 var jzcg = $("#model_jzcg").val();
		 var zljg = $("#model_zljg").val();
		 var zltj = $("#model_zltj").val();
		 var bctj = $("#model_bctj").val();
		 if(bwcp=="0"&&jzcg=="0"&&zljg=="0"&&zltj==""&&bctj==""){
			 alertFunc("限制信息不能为空");
			 return;
		 }
		 $("#lrrid").val(userInfo.userid);
		 $("#lrrmc").val(userInfo.username);
		 $("#lrsj").val(nowDate());
		 var params =$("#form_cpxzxx").serializeObject();
		 var detail = ajaxResult("get",false,ProjectName+"/collimit.saveColLimit.do",params);
		 if(detail.status=="ok"){
			 alert("保存成功");
			 $("#cpxzxxModal").modal('hide');
			 refreshTable('#cpxzTable','#form1');
		 }else{
			 alert("保存失败")
		 }
			
	 });
})
function showDetail(zdjh){
	var detail = ajaxResult("get",false,ProjectName+"/collimit.getColLimitByZdjh.do",{"zdjh":zdjh});
	 //填充藏品信息到表单
	 if(detail.status=="ok"){
		 loadData("#form_cpxzxx",detail.data); 
		 $('#zdjh').attr("readonly","readonly");
		 if(detail.data.editable==true){//可编辑
			 $("#save_btn").show();
			 $('#model_zljg').removeAttr("readonly");
			 $('#model_zltj').removeAttr("readonly");
			 $('#model_bctj').removeAttr("readonly");
			 $('#model_jzcg').removeAttr("disabled");
			 $('#model_bwcp').removeAttr("disabled");
		 }else{//只读
			 $("#save_btn").hide();
			 $('#model_zljg').attr("readonly","readonly");
			 $('#model_zltj').attr("readonly","readonly");
			 $('#model_bctj').attr("readonly","readonly");
			 $('#model_jzcg').attr("disabled","disabled");
			 $('#model_bwcp').attr("disabled","disabled");
		 }
		 $("#cpxzxxModal").modal('show');
	 }
}
var dxcpTitle = [{
    checkbox: true,
    formatter : function(value, row, index) {
    	if (row.editable == false)
            return {
                disabled : true,//设置是否可用
                checked : false//设置选中
            };
        else
            return {
            disabled : false,//设置是否可用
            checked : false//设置选中
        };
	}
}, {
	field: 'zdjh',
	title: '藏品总登记号',
	formatter: function(value, row, index) {
		return '<a href="#" onclick="showDetail(\''+row.zdjh+'\')">'+value+'</a>';
	}
},{
	field: 'flh',
	title: '分类号'
},{
	field: 'cpmc',
	title: '藏品名称',
	formatter: function(value, row, index) {
		return '<a href="#" onclick="showDetail(\''+row.zdjh+'\')">'+value+'</a>';
	}
},{
	field: 'cpfl1',
	title: '藏品分类'
},{
	field: 'cpjb',
	title: '藏品级别'
},{//藏品总登记号、分类号、藏品名称、藏品分类、藏品级别、是否限制出国、是否濒危、展览间隔（天）、展览条件、保存条件
	field: 'jzcg',
	title: '是否限制出国'
},{ 
	field: 'bwcp',
	title: '是否濒危'
},{
	field: 'zljg',
	title: '展览间隔（天）',
	formatter: function(value, row, index) {
		if(value==0)
		return '无';
		else
			return value;
	}
},{
	field: 'zltj',
	title: '展览条件'
},{
	field: 'bctj',
	title: '保存条件'
}]